﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using KeepFocused;

namespace KeepFocused
{
    /// <summary>
    /// The form for user to input session name and activity description.
    /// </summary>
    public partial class TaskForm : Form
    {
        private Database db = null;
        private Model.Session currentSession = null;
        private string TaskFileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\KeepFocusedTask.txt";
        
        //TODO: create a new class "Options" and move this into it.
        private string ConnectionString = @"Data Source=D:\work\opensource\keepfocused\data\logs.sdf";

        public TaskForm()
        {
            InitializeComponent();

            db = new Database(this.ConnectionString);
            string desc = null;
            int id = db.GetUnfinishedSession(out desc);
            if (id != 0)
                this.currentSession = db.GetSession(id);

            if (this.currentSession != null)
            {
                this.txtSession.Text = this.currentSession.Description;
            }
        }

        /// <summary>
        /// OK button to start a new activity. This will create a session if it's new.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.txtSession.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Input session description");
                return;
            }

            if (this.txtTask.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Input activity description");
                return;
            }

            if (this.currentSession == null)
            {
                int session_id = db.StartNewSession(this.txtSession.Text);
                int activity_id = db.StartNewActivity(session_id, this.txtTask.Text);
                this.labelIndex.Text = "1 of 4 in session";
            }
            else
            {
                int activity_id = db.StartNewActivity(this.currentSession.ID, this.txtTask.Text);
                //
            }

            this.Close();
        }

        /// <summary>
        /// View logs. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnViewDataFile_Click(object sender, EventArgs e)
        {
            if (File.Exists(TaskFileName))
                System.Diagnostics.Process.Start(TaskFileName);
            else
            {
                string message = "Log file cannot be read.\r\nThis may be because you are running Keep Focused very first time or you may have deleted the log file.\r\nLogfile will be automatically created when you run the application again.";
                MessageBox.Show(message, "Keep Focused", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Show option dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOptions_Click(object sender, EventArgs e)
        {
            new Options().ShowDialog();
        }
        
        private void txtTask_TextChanged(object sender, EventArgs e)
        {
            if (this.txtTask.Text.Trim().Length == 0)
                this.btnOK.Enabled = false;
            else
                this.btnOK.Enabled = true;
        }

        private void txtSession_TextChanged(object sender, EventArgs e)
        {
            if (this.txtSession.Text.Trim().Length == 0)
                this.btnOK.Enabled = false;
            else
                this.btnOK.Enabled = true;
        }

        private void TaskForm_Load(object sender, EventArgs e)
        {
            this.btnOK.Enabled = false;
        }
    }
}
